package pkg_member

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/pkg_member"
	pkg_memberReq "github.com/flipped-aurora/gin-vue-admin/server/model/pkg_member/request"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type ActivateApi struct {
}

var activateService = service.ServiceGroupApp.Pkg_memberServiceGroup.ActivateService

// CreateActivate 创建活动管理/发布
// @Tags Activate
// @Summary 创建活动管理/发布
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body pkg_member.Activate true "创建活动管理/发布"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /activate/createActivate [post]
func (activateApi *ActivateApi) CreateActivate(c *gin.Context) {
	var activate pkg_member.Activate
	err := c.ShouldBindJSON(&activate)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	activate.CreatedBy = utils.GetUserID(c)

	if err := activateService.CreateActivate(&activate); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteActivate 删除活动管理/发布
// @Tags Activate
// @Summary 删除活动管理/发布
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body pkg_member.Activate true "删除活动管理/发布"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /activate/deleteActivate [delete]
func (activateApi *ActivateApi) DeleteActivate(c *gin.Context) {
	ID := c.Query("ID")
	userID := utils.GetUserID(c)
	if err := activateService.DeleteActivate(ID, userID); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteActivateByIds 批量删除活动管理/发布
// @Tags Activate
// @Summary 批量删除活动管理/发布
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /activate/deleteActivateByIds [delete]
func (activateApi *ActivateApi) DeleteActivateByIds(c *gin.Context) {
	IDs := c.QueryArray("IDs[]")
	userID := utils.GetUserID(c)
	if err := activateService.DeleteActivateByIds(IDs, userID); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateActivate 更新活动管理/发布
// @Tags Activate
// @Summary 更新活动管理/发布
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body pkg_member.Activate true "更新活动管理/发布"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /activate/updateActivate [put]
func (activateApi *ActivateApi) UpdateActivate(c *gin.Context) {
	var activate pkg_member.Activate
	err := c.ShouldBindJSON(&activate)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	activate.UpdatedBy = utils.GetUserID(c)

	if err := activateService.UpdateActivate(activate); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindActivate 用id查询活动管理/发布
// @Tags Activate
// @Summary 用id查询活动管理/发布
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query pkg_member.Activate true "用id查询活动管理/发布"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /activate/findActivate [get]
func (activateApi *ActivateApi) FindActivate(c *gin.Context) {
	ID := c.Query("ID")
	if reactivate, err := activateService.GetActivate(ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"reactivate": reactivate}, c)
	}
}

// GetActivateList 分页获取活动管理/发布列表
// @Tags Activate
// @Summary 分页获取活动管理/发布列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query pkg_memberReq.ActivateSearch true "分页获取活动管理/发布列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /activate/getActivateList [get]
func (activateApi *ActivateApi) GetActivateList(c *gin.Context) {
	var pageInfo pkg_memberReq.ActivateSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := activateService.GetActivateInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

// GetActivatePublic 不需要鉴权的活动管理/发布接口
// @Tags Activate
// @Summary 不需要鉴权的活动管理/发布接口
// @accept application/json
// @Produce application/json
// @Param data query pkg_memberReq.ActivateSearch true "分页获取活动管理/发布列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /activate/getActivateList [get]
func (activateApi *ActivateApi) GetActivatePublic(c *gin.Context) {
	// 此接口不需要鉴权
	// 示例为返回了一个固定的消息接口，一般本接口用于C端服务，需要自己实现业务逻辑
	response.OkWithDetailed(gin.H{
		"info": "不需要鉴权的活动管理/发布接口信息",
	}, "获取成功", c)
}
